//
//  UIViewController+ILGUIKit.m
//  ILGDemo
//
//  Created by zhangtongle-Pro on 2017/11/16.
//  Copyright © 2017年 Happy. All rights reserved.
//

#import "UIViewController+ILGUIKit.h"
#import "ILGBaseVC.h"

static int age = 10;
#define kNavigationBackBarColor [UIColor redColor]


@implementation UIViewController (ILGUIKit)

+ (void)load {
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(viewDidLoad)), class_getInstanceMethod([self class], @selector(ilg_viewDidLoad)));
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(viewWillAppear:)), class_getInstanceMethod([self class], @selector(ilg_viewWillAppear:)));
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(viewWillDisappear:)), class_getInstanceMethod([self class], @selector(ilg_viewWillDisappear:)));
}

- (void)ilg_viewDidLoad {
    [self ilg_viewDidLoad];
    
    ILGLog(@"current class = %@", [self class]);
}

- (void)ilg_viewWillAppear:(BOOL)animated
{
    [self ilg_viewWillAppear:animated];
    
    if (self.ilg_isNavigationBarHidded) {
        [self.navigationController setNavigationBarHidden:YES animated:animated];
    }
    
    if (self.ilg_isTabBarHidded) {
        [self.tabBarController.tabBar setHidden:YES];
    }

//    NSDictionary *dic = @{NSForegroundColorAttributeName:kNavigationBackBarColor,NSFontAttributeName:[UIFont systemFontOfSize:18], NSShadowAttributeName:({
//        NSShadow *shadow = [NSShadow new];
//        [shadow setShadowOffset:CGSizeMake(0.f, 0.f)];
//        shadow;
//    })};
//    self.navigationController.navigationBar.titleTextAttributes = dic;
//
//    if (!self.navigationItem.leftBarButtonItem && !self.navigationItem.leftBarButtonItems && [self.navigationController viewControllers].count > 1) {
//        UIImage *image = [UIImage imageNamed:@"nav_back_normal.png"];
//        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:image
//                                                                     style:UIBarButtonItemStylePlain target:self action:@selector(navigationBackButtonDidTap)];
//        self.navigationItem.leftBarButtonItem = backItem;
//
//        self.navigationController.navigationBar.tintColor = kNavigationBackBarColor;
//        if ([self isKindOfClass:[ILGBaseVC class]]) {
//
//        } else {
//
//        }
//    }
    
    if ([self isKindOfClass:[UINavigationController class]]) {
    }
}

- (void)ilg_viewWillDisappear:(BOOL)animated
{
    [self ilg_viewWillDisappear:animated];
    
    if (self.ilg_isNavigationBarHidded) {
        [self.navigationController setNavigationBarHidden:NO animated:animated];
    }
    
    if (self.ilg_isTabBarHidded) {
        [self.tabBarController.tabBar setHidden:NO];
    }
}

- (void)navigationBackButtonDidTap
{
    [self.navigationController popViewControllerAnimated:YES];
}


+ (instancetype)ilg_VCFromSB:(NSString *)SBName {
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:SBName bundle:nil];
    return [storyBoard instantiateViewControllerWithIdentifier:NSStringFromClass([self class])];
}

+ (instancetype)ilg_VCFromXIB {
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
}

- (void)ilg_setNavigationBarBGColor:(UIColor *)color {
    if([color isEqual:[UIColor clearColor]]) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    } else if(color) {
        self.navigationController.navigationBar.barTintColor = color;
    } else {
        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:nil];
    }
}

- (BOOL)ilg_isNavigationBarHidded
{
    NSNumber *number = objc_getAssociatedObject(self, _cmd);
    return [number boolValue];
}

- (void)setIlg_isNavigationBarHidded:(BOOL)ilg_isNavigationBarHidded
{
    objc_setAssociatedObject(self, @selector(ilg_isNavigationBarHidded), @(ilg_isNavigationBarHidded), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)ilg_isTabBarHidded
{
    NSNumber *number = objc_getAssociatedObject(self, _cmd);
    return [number boolValue];
}

- (void)setIlg_isTabBarHidded:(BOOL)ilg_isTabBarHidded
{
    objc_setAssociatedObject(self, @selector(ilg_isTabBarHidded), @(ilg_isTabBarHidded), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end


#pragma mark - 模态跳转，非全屏显示
#import <objc/runtime.h>

@interface UIViewController () <UIViewControllerTransitioningDelegate>
@end

@implementation UIViewController (ILGCategory)

- (void)setVcHeight:(CGFloat)vcHeight {
    objc_setAssociatedObject(self, @"vcHeight", @(vcHeight), OBJC_ASSOCIATION_ASSIGN);
    
    self.modalPresentationStyle = UIModalPresentationCustom;
    self.transitioningDelegate = self;
}
- (CGFloat)vcHeight {
    return [objc_getAssociatedObject(self, @"vcHeight") floatValue];
}

- (void)setColorForBlank:(UIColor *)colorForBlank {
    objc_setAssociatedObject(self, @"colorForBlank", colorForBlank, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIColor *)colorForBlank {
    UIColor *color = objc_getAssociatedObject(self, @"colorForBlank");
    if (!color) {
        color = [UIColor whiteColor];
    }
    return color;
}

- (void)setAlphaForBlank:(CGFloat)alphaForBlank {
    objc_setAssociatedObject(self, @"alphaForBlank", @(alphaForBlank), OBJC_ASSOCIATION_ASSIGN);
}
- (CGFloat)alphaForBlank {
    CGFloat alpha = [objc_getAssociatedObject(self, @"alphaForBlank") floatValue];
    if (alpha == 0) {
        alpha = 0.015;
    }
    return alpha;
}

- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(nullable UIViewController *)presenting sourceViewController:(UIViewController *)source {
    UIPresentationController *presentVC = nil;
    
    Class class = NSClassFromString(@"ILGPresentationController");
    if (self.vcHeight && class) {
        presentVC = [(UIPresentationController *)[class alloc] initWithPresentedViewController:presented presentingViewController:presenting];
        [presentVC setValue:@(self.vcHeight) forKey:@"vcHeight"];
        [presentVC setValue:@(self.alphaForBlank) forKey:@"alphaForBlank"];
        [presentVC setValue:self.colorForBlank forKey:@"colorForBlank"];
    } else {
        presentVC = [[UIPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
    }
    
    return presentVC;
}

@end


@interface ILGPresentationController ()
@property(nonatomic,strong) UIButton *dimView; //黑色半透明蒙版
@end

@implementation ILGPresentationController

- (void)containerViewWillLayoutSubviews {
    [super containerViewWillLayoutSubviews];
    
    //presentedView即我们主要内容显示的区域
    UIView *mainView = self.presentedView;
    
    [self.containerView addSubview:mainView];
    
    //弹出Controller之后,上面空白的部分加个蒙版,点击后可以dismiss掉控制器
    [self.containerView insertSubview:self.dimView atIndex:0];
    
    //动画改变透蒙版明度
    [UIView animateWithDuration:0.25 animations:^{
        self.dimView.alpha = self.alphaForBlank;
    }];
    
    [self frameSetup];
}


- (void)frameSetup {
    CGFloat x,y,w,h;
    
    x = 0;
    // 屏幕高度 - 要弹出的控制器的高度
    y = [UIScreen mainScreen].bounds.size.height - self.vcHeight;
    // 要弹出的控制器的宽度
    w = [UIScreen mainScreen].bounds.size.width;
    // 要弹出的控制器的高度
    h = self.vcHeight;
    self.presentedView.frame = CGRectMake(x, y, w, h);
    
    y = 0;
    h = [UIScreen mainScreen].bounds.size.height;
    self.dimView.frame = CGRectMake(x,  y,  w,  h);
}

- (UIButton *)dimView {
    if (!_dimView) {
        _dimView = [UIButton buttonWithType:UIButtonTypeCustom];
        _dimView.backgroundColor = _colorForBlank;
        _dimView.alpha = 0;
        [_dimView addTarget:self action:@selector(tapClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _dimView;
}

- (void)tapClick {
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
    [UIView animateWithDuration:0.25 animations:^{
        self.dimView.alpha = 0;
    }];
}

@end
